local wuhun = fk.CreateSkill {

  name = "joy__wuhun",

  tags = { Skill.Compulsory, },

}



wuhun:addEffect(fk.Damaged, {
  name = "joy__wuhun",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.Damaged,fk.AfterDying, fk.Death},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self, false, true) then
      if event == fk.Damaged then
        return data.from and not data.from.dead and not player.dead
      elseif event == fk.Death and player:getMark("joy__wuhun-turn") then
        return false
      else
        local availableTargets = {}
        local n = 0
        for _, p in ipairs(player.room.alive_players) do
          if p:getMark("@joy__nightmare") > n then
            availableTargets = {}
            table.insert(availableTargets, p.id)
            n = p:getMark("@joy__nightmare")
          elseif p:getMark("@joy__nightmare") == n and n ~= 0 then
            table.insert(availableTargets, p.id)
          end
        end
        if #availableTargets > 0 then
          event:setCostData(self, availableTargets)
          return true
        end
      end
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damaged then
      room:addPlayerMark(data.from, "@joy__nightmare", data.damage)
    else
      local p_id
      if #event:getCostData(self) > 1 then
        p_id = room:askForChoosePlayers(player, event:getCostData(self), 1, 1, "#joy__wuhun-choose", wuhun.name, false)[1]
      else
        p_id = event:getCostData(self)[1]
      end
      local judge = {
        who = room:getPlayerById(p_id),
        reason = wuhun.name,
        pattern = "peach,god_salvation|.",
      }
      room:judge(judge) 
      if event == fk.Death then
        player.room:setPlayerMark(player,"joy__wuhun-turn",1)
      end
      if judge.card.name == "peach" or judge.card.name == "god_salvation" then return false end
      local p = room:getPlayerById(p_id)
      room:loseHp(p,5,wuhun.name)
    end
  end,
})
wuhun:addEffect(fk.AfterDying, {
  name = "joy__wuhun",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.Damaged,fk.AfterDying, fk.Death},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self, false, true) then
      if event == fk.Damaged then
        return data.from and not data.from.dead and not player.dead
      elseif event == fk.Death and player:getMark("joy__wuhun-turn") then
        return false
      else
        local availableTargets = {}
        local n = 0
        for _, p in ipairs(player.room.alive_players) do
          if p:getMark("@joy__nightmare") > n then
            availableTargets = {}
            table.insert(availableTargets, p.id)
            n = p:getMark("@joy__nightmare")
          elseif p:getMark("@joy__nightmare") == n and n ~= 0 then
            table.insert(availableTargets, p.id)
          end
        end
        if #availableTargets > 0 then
          event:setCostData(self, availableTargets)
          return true
        end
      end
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damaged then
      room:addPlayerMark(data.from, "@joy__nightmare", data.damage)
    else
      local p_id
      if #event:getCostData(self) > 1 then
        p_id = room:askForChoosePlayers(player, event:getCostData(self), 1, 1, "#joy__wuhun-choose", wuhun.name, false)[1]
      else
        p_id = event:getCostData(self)[1]
      end
      local judge = {
        who = room:getPlayerById(p_id),
        reason = wuhun.name,
        pattern = "peach,god_salvation|.",
      }
      room:judge(judge) 
      if event == fk.Death then
        player.room:setPlayerMark(player,"joy__wuhun-turn",1)
      end
      if judge.card.name == "peach" or judge.card.name == "god_salvation" then return false end
      local p = room:getPlayerById(p_id)
      room:loseHp(p,5,wuhun.name)
    end
  end,
})
wuhun:addEffect(fk.Death, {
  name = "joy__wuhun",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.Damaged,fk.AfterDying, fk.Death},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self, false, true) then
      if event == fk.Damaged then
        return data.from and not data.from.dead and not player.dead
      elseif event == fk.Death and player:getMark("joy__wuhun-turn") then
        return false
      else
        local availableTargets = {}
        local n = 0
        for _, p in ipairs(player.room.alive_players) do
          if p:getMark("@joy__nightmare") > n then
            availableTargets = {}
            table.insert(availableTargets, p.id)
            n = p:getMark("@joy__nightmare")
          elseif p:getMark("@joy__nightmare") == n and n ~= 0 then
            table.insert(availableTargets, p.id)
          end
        end
        if #availableTargets > 0 then
          event:setCostData(self, availableTargets)
          return true
        end
      end
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damaged then
      room:addPlayerMark(data.from, "@joy__nightmare", data.damage)
    else
      local p_id
      if #event:getCostData(self) > 1 then
        p_id = room:askForChoosePlayers(player, event:getCostData(self), 1, 1, "#joy__wuhun-choose", wuhun.name, false)[1]
      else
        p_id = event:getCostData(self)[1]
      end
      local judge = {
        who = room:getPlayerById(p_id),
        reason = wuhun.name,
        pattern = "peach,god_salvation|.",
      }
      room:judge(judge) 
      if event == fk.Death then
        player.room:setPlayerMark(player,"joy__wuhun-turn",1)
      end
      if judge.card.name == "peach" or judge.card.name == "god_salvation" then return false end
      local p = room:getPlayerById(p_id)
      room:loseHp(p,5,wuhun.name)
    end
  end,
})

return wuhun